//
//  MPlusHumanitiesListViewController.m
//  medicineplus
//
//  Created by gufei on 15-5-19.
//  Copyright (c) 2015年 chn_ruby@126.cn. All rights reserved.
//

#import "MPlusHumanitiesListViewController.h"
#import "MPlusHumanitiesTableViewCell.h"
#import "MPlusHumanitiesTableViewCell2.h"
#import "AdView.h"
#import "MPlusDefine.h"
#import "MPlusString.h"

@interface MPlusHumanitiesListViewController ()<UITableViewDataSource,UITableViewDelegate>  {
    AdView * adView;
}
@property (nonatomic, strong) NSMutableArray *aHumanitiesListArray;
@property (nonatomic, retain) UITableView *aTableView;

@end

@implementation MPlusHumanitiesListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self setNavigationTitle:kNavTitleHumanities];
    }
    return self;
}

- (void)viewDidLoad {
    // 隐藏TabBar
    self.hidesBottomBarWhenPushed = YES;
    // Keeps tab bar below navigation bar on iOS 7.0+
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    // Do any additional setup after loading the view from its nib.
    _aHumanitiesListArray = [@[@"photo_sample_01",
                         @"photo_sample_02",
                         @"photo_sample_03",
                         @"photo_sample_04",
                         @"photo_sample_05",
                         @"photo_sample_06",
                         @"photo_sample_07",
                         @"photo_sample_08",] mutableCopy];
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
    // 设置tableView的数据源
    tableView.dataSource = self;
    // 设置tableView的委托
    tableView.delegate = self;
    self.aTableView = tableView;
    self.aTableView.tableHeaderView=[self createScrollView];
    [self.view addSubview:self.aTableView];
    // 去除多余的分割线
    [self setExtraCellLineHidden:_aTableView];
    [super viewDidLoad];
}

- (AdView *)createScrollView {
    NSArray *imagesURL = @[
                           @"http://d.hiphotos.baidu.com/image/w%3D400/sign=4e73be8ab0fb43161a1f7b7a10a44642/3b87e950352ac65c6326b832f9f2b21193138a54.jpg",
                           @"http://d.hiphotos.baidu.com/image/w%3D400/sign=e173a8e1b27eca80120538e7a1229712/cf1b9d16fdfaaf51a0bc8f7d8e5494eef11f7a90.jpg",
                           @"http://c.hiphotos.baidu.com/image/w%3D400/sign=dfb029cab0fb43161a1f7b7a10a54642/3b87e950352ac65cf2e52f72f9f2b21192138acb.jpg"
                           ];
    
    // 情景三：图片配文字(可选)
    NSArray *titles = @[@"AAOS 单髓内钉效果最好",
                        @"AAOS 单髓内钉效果最好",
                        @"AAOS 单髓内钉效果最好",
                        ];
    
    //如果你的这个广告视图是添加到导航控制器子控制器的View上,请添加此句,否则可忽略此句
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    adView = [AdView adScrollViewWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 180) imageLinkURL:imagesURL placeHoderImageName:@"placeHoder.jpg" pageControlShowStyle:UIPageControlShowStyleLeft];
    
    //    是否需要支持定时循环滚动，默认为YES
    //    adView.isNeedCycleRoll = YES;
    
    [adView setAdTitleArray:titles withShowStyle:AdTitleShowStyleRight];
    //    设置图片滚动时间,默认3s
    //    adView.adMoveTime = 2.0;
    
    //图片被点击后回调的方法
    adView.callBack = ^(NSInteger index,NSString * imageURL) {
        
    };
    return adView;;
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return _aHumanitiesListArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger row = [indexPath row];
    NSString *cellIdentifier = [NSString stringWithFormat:@"%lu", (unsigned long)row];
    MPlusHumanitiesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    MPlusHumanitiesTableViewCell2 *cell2 = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil || cell2 == nil) {
        cell = [[MPlusHumanitiesTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                             reuseIdentifier:cellIdentifier];

        cell2 = [[MPlusHumanitiesTableViewCell2 alloc] initWithStyle:UITableViewCellStyleDefault
                                                   reuseIdentifier:cellIdentifier];
    }
    if (row%2==0) {
        NSString *title = @"AAOS：单纯髓内钉治疗临界病理骨折效果良好";
        [cell.aTitle verticalUpAlignmentWithText:title maxHeight:22];
        cell.aImage1.image = [UIImage imageNamed:_aHumanitiesListArray[row]];
        cell.aImage2.image = [UIImage imageNamed:_aHumanitiesListArray[row]];
        cell.aImage3.image = [UIImage imageNamed:_aHumanitiesListArray[row]];
        return cell;
    } else {
        NSString *title = @"AAOS：单纯髓内钉治疗临界病理骨折效果良好";
        [cell2.aTitle verticalUpAlignmentWithText:title maxHeight:44];
        NSString *intro = @"介绍：针对临界性病理骨折预防性外科固定的目的就是为了减少疼痛，预防骨折，利于快速恢复";
        [cell2.aIntro verticalUpAlignmentWithText:intro maxHeight:60];
        cell2.aImage.image = [UIImage imageNamed:_aHumanitiesListArray[row]];
        return cell2;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //    DetailViewController *detailVC = [[DetailViewController alloc]initWithNibName:@"DetailViewController" bundle:nil];
    //    [self.navigationController pushViewController:detailVC animated:YES];
    //消除cell选择痕迹
    [self performSelector:@selector(deselect) withObject:nil afterDelay:0.5f];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSUInteger row = [indexPath row];
    if (row%2==0) {
        return 164;
    }
    return 132;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -
#pragma mark Table View setExtraCellLineHidden Methods
- (void)setExtraCellLineHidden: (UITableView *)tableView {
    UIView *view =[ [UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
    [tableView setTableHeaderView:view];
}

#pragma mark -
#pragma mark Table View Cell deselect Methods
- (void) deselect {
    [_aTableView deselectRowAtIndexPath:[_aTableView indexPathForSelectedRow] animated:YES];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
